package org.qiandaosystem.qiandaosystemsource.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.qiandaosystem.qiandaosystemsource.common.ApiResponse;
import org.qiandaosystem.qiandaosystemsource.service.StudentService;
import org.qiandaosystem.qiandaosystemsource.service.QrCodeSignService;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.media.Schema;

@RestController
@RequestMapping("/api/student")
@RequiredArgsConstructor
@SecurityRequirement(name = "bearerAuth")
public class StudentController {
    
    private final StudentService studentService;
    private final QrCodeSignService qrCodeSignService;

    @Data
    public static class CodeSignRequest {
        @Schema(description = "4位数签到码", example = "1234")
        private String signCode;
    }

    @Data
    public static class QrCodeSignRequest {
        private String qrCodeContent;
    }

    @Operation(summary = "查询待签到的活动")
    @GetMapping("/pending-sign-activities")
    public ApiResponse<?> getPendingSignActivities(
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.getPendingSignActivities(jwt);
    }

    @Operation(summary = "查询已过期未签到的记录")
    @GetMapping("/expired-unsigned-activities")
    public ApiResponse<?> getExpiredUnsignedActivities(
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.getExpiredUnsignedActivities(jwt);
    }

    @Operation(summary = "查询已加入的教师信息")
    @GetMapping("/my-teacher")
    public ApiResponse<?> getMyTeacher(
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.getMyTeacher(jwt);
    }

    @Operation(summary = "查看老师的签到活动")
    @GetMapping("/sign-activities")
    public ApiResponse<?> getTeacherSignActivities(
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.getTeacherSignActivities(jwt);
    }

    @Operation(summary = "参与签到")
    @PostMapping("/sign-activities/{activityId}/sign")
    public ApiResponse<?> signActivity(
            @PathVariable Integer activityId,
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.signActivity(jwt, activityId);
    }

    @Operation(summary = "查看个人签到记录")
    @GetMapping("/sign-records")
    public ApiResponse<?> getSignRecords(
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.getSignRecords(jwt);
    }

    @Operation(summary = "签到码签到")
    @PostMapping("/sign-activities/{activityId}/code-sign")
    public ApiResponse<?> codeSign(
            @PathVariable Integer activityId,
            @RequestBody CodeSignRequest request,
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.codeSign(jwt, activityId, request.getSignCode());
    }

    @Operation(summary = "扫码签到")
    @PostMapping("/sign/qrcode")
    public ApiResponse<?> qrCodeSign(
            @RequestBody QrCodeSignRequest request,
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return qrCodeSignService.verifyQrCode(jwt, request.getQrCodeContent());
    }
    @Data
    public static class GestureSignRequest {
        @Schema(description = "九宫格手势密码", example = "1,2,3,4")
        private String gesturePoint;
    }

    @Operation(summary = "九宫格手势签到")
    @PostMapping("/sign-activities/{activityId}/gesture-sign")
    public ApiResponse<?> gestureSign(
            @PathVariable Integer activityId,
            @RequestBody GestureSignRequest request,
            @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return studentService.gestureSign(jwt, activityId, request.getGesturePoint());
    }
    @Data
    public static class LocationSignRequest {
        @Schema(description = "学生的纬度", example = "39.9042")
        private Double lat;

        @Schema(description = "学生的经度", example = "116.4074")
        private Double lon;
    }
    @Operation(summary = "位置签到")
    @PostMapping("/sign-activities/{activityId}/location-sign")
    public ApiResponse<?> locationSign(
            @PathVariable Integer activityId,
            @RequestBody LocationSignRequest request,
            @RequestHeader("Authorization") String token
    ) {
        String jwt = token.substring(7);
        return studentService.locationSign(
                jwt,
                activityId,
                request.getLat(),
                request.getLon()
        );
    }
} 